package com.controller;

import com.pojo.Account;
import com.pojo.DataAccount;
import com.pojo.Family;
import com.pojo.Operator;
import com.service.Impl.OperatorServiceImpl;
import com.service.OperatorService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/operators")
public class OperatorController {

       @Autowired
       OperatorService Op=new OperatorServiceImpl();

       //操作员登入实现
       @PostMapping("login")
       public Result login(@RequestBody Operator operator){
              Operator login = Op.login(operator);
              boolean flag=login!=null;
              return  new Result(flag ? Code.select_Ok:Code.select_ERR, flag ? login.getId() : null);
       }

       /**
        * 查看某地区所有户主信息
        * @param id
        * @return
        */
       @GetMapping("/account/{id}")
       public Result selectAccount(@PathVariable Integer id) {
              List<Account> allAccounts = Op.getAllAccount(id);
              List<DataAccount> dataAccounts=new ArrayList<>();

              for(Account account:allAccounts){
                     DataAccount dataAccount=new DataAccount();

                     dataAccount.setPoorKind(Op.selectPoorKind(account.getPoorKind()));

                     dataAccount.setId(account.getId());

                     dataAccount.setIdentity(account.getIdentity());

                     dataAccount.setSex(account.getSex());

                     dataAccount.setPhone(account.getPhone());

                     dataAccount.setHomeArea(account.getHomeArea());

                     dataAccount.setAcName(account.getAcName());

                     //根据户主ID查找家庭人口数
                     dataAccount.setFamilyAccounts(Op.selectSum(account.getId()));

                     dataAccounts.add(dataAccount);

              }

              Integer code=!dataAccounts.isEmpty() ? Code.select_Ok:Code.select_ERR;

              String message=!dataAccounts.isEmpty() ? "":"查询失败,没有找到相关结果";

              return new Result(code,dataAccounts,message);
       }

       /**
        * 添加户主信息
        * @param dataAccount
        * @return
        */
       @PostMapping("/account")
       public Result addAccount(@RequestBody DataAccount dataAccount){
              log.info("{}",dataAccount);
              Account account=new Account();
              account.setPoorKind(Op.selectPoorId(dataAccount.getPoorKind()));
              account.setAreaId(dataAccount.getId());
              account.setIdentity(dataAccount.getIdentity());
              account.setPhone(dataAccount.getPhone());
              account.setHomeArea(dataAccount.getHomeArea());
              account.setSex(dataAccount.getSex());
              account.setAcName(dataAccount.getAcName());
              account.setAreaId(dataAccount.getAreaId());


              boolean flag = Op.addAccount(account);

              return  new Result(flag ? Code.add_Ok:Code.add_ERR,flag);
       }

       /**
        * 删除户主
        * @param id
        * @return
        */
       @DeleteMapping("/account/{id}")
       public Result deletePoor(@PathVariable Integer id) {
              boolean flag = Op.deleteAccount(id);
              return  new Result(flag ? Code.delete_Ok:Code.delete_ERR,flag);
       }

       /**
        * 修改
        * @param dataAccount
        * @return
        */
       @PutMapping("/account")
       public Result updateAccount(@RequestBody DataAccount dataAccount){


              Account account=new Account();
              account.setPoorKind(Op.selectPoorId(dataAccount.getPoorKind()));
              account.setAreaId(dataAccount.getAreaId());
              account.setId(dataAccount.getId());
              account.setIdentity(dataAccount.getIdentity());
              account.setPhone(dataAccount.getPhone());
              account.setHomeArea(dataAccount.getHomeArea());
              account.setSex(dataAccount.getSex());
              account.setAcName(dataAccount.getAcName());

              boolean flag = Op.updateAccount(account);

              return  new Result(flag ? Code.update_Ok:Code.update_ERR,flag);
       }

       /**
        * 查询家庭成员
        * @param id
        * @return
        */
       @GetMapping("/family/{id}")
       public Result selectByAccountId(@PathVariable Integer id){
              List<Family> families = Op.selectByAccountId(id);
              Integer code=!families.isEmpty() ? Code.select_Ok:Code.select_ERR;

              String message=!families.isEmpty() ? "":"查询失败,没有找到相关结果";

              return new Result(code,families,message);
       }

       /**
        * 添加家庭成员
        * @param family
        * @return
        */
       @PostMapping("/family")
       public Result addFamily(@RequestBody Family family){
              boolean flag = Op.addFamily(family);
              return  new Result(flag ? Code.add_Ok:Code.add_ERR,flag);
       }

       /**
        * 修改
        * @param family
        * @return
        */
       @PutMapping("/family")
       public Result updateFamily(@RequestBody Family family){
              boolean flag = Op.updateFamily(family);
              return  new Result(flag ? Code.update_Ok:Code.update_ERR,flag);
       }

       @DeleteMapping("/family/{id}")
       public Result deleteFamily(@PathVariable Integer id){
              boolean flag = Op.deleteFamily(id);
              return  new Result(flag ? Code.delete_Ok:Code.delete_ERR,flag);
       }

}
